<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/Request.class.php, $Author:  $ */

/**
 * Class to acces request paramteres (GET, POST and URL-encoded)
 * @package core
 */

class Request
{
    // DATA MEMBERS

    /** Arguments of the request
     * @var array
     * @access private
     */
    var $_aArgs;

    /** Action Name
     * @var string
     * @access private
     */
    var $_sAction;


    /** Url wrapper object
     * @var string
     * @access private
     */
    var $_oUrl;

    /** Current area parts.
     * @var array
     */
    var $_aParts;

    /** Menu
     * @var unknown_type
     */
    var $_aMenu;

    /** Current area.
     * @var string
     */
    var $_sArea;

    /** Current part.
     * @var string
     */
    var $_sPart;

    /** Current section.
     * @var string
     */
    var $_sSect;


    /** Construct a new Request object.
     * @param Url $oUrl url object for curren url
     */
    function Request($oUrl)
    {
        $this->_aArgs = $this->_trim(array_merge($_GET, $oUrl->getParams(), $_POST));
        $mAction = $this->get('act');
		if (is_array($mAction))
        {
            $key = array_keys($mAction);
            $mAction = array_pop($key);
        }
        $this->_sAction = $mAction;
        $this->_oUrl = $oUrl;
    }

    /** Forward to given url.
     * @param string $sLogicUrl logical URL name ('admin.login' etc.)
     * @param array  $aParams   paramters (name=>value)
     * @param mixed  $mMes      message(s) that should be displayed on target page
     * @param bool   $bErr      is error message(s)
     */
    function gotoUrl($sLogicUrl, $aParams=array(), $mMes=array(), $bErr=false)
    {
        $this->forward(App::makeUrl($sLogicUrl, $aParams), $mMes, $bErr);
    }

    /** Forward request to new URL. ( sends header and call exit() ).
     * @param string $sUrl      absolute URL to forward
     * @param mixed  $mMes      message or array of messages
     * @param string $sLogicUrl logical url corresponding to forwarder (For store messages)
     * @return void
     */
    function forward($sUrl, $mMes=array(), $bErr=false, $sLogicUrl='')
    {
        if (!is_array($mMes) && $mMes)
            $mMes = array($mMes);

        if ('/' !== substr($sUrl, -1) &&
            '.html' !== substr($sUrl, -5) &&
            '.php' !== substr($sUrl, -4) )
            $sUrl .= '/';

        $sLogicUrl = $sLogicUrl ? $sLogicUrl : $sUrl;
        if ($mMes)
            $_SESSION[$sLogicUrl]['err'] = array($mMes, $bErr,  'aSessErr');
        header('Location: '.$sUrl);
        exit;
    }

    /** Get action name.
     * @return string action name
     */
    function getAction($sDefault='')
    {
        return ($this->_sAction ? $this->_sAction : $sDefault);
    }

    function set($sName, $sValue)
    {
    	$this->_aArgs[$sName] = $sValue;
    }

    /** Retrieve parameter value from request.
     * @param string $sName name of argument
     * @param string $sDef  optional default value
     * @return string parameter value
     */
    function get($sName, $mDef='')
    {
        return (isset($this->_aArgs[$sName]) ? $this->_aArgs[$sName] : $mDef);
    }

    /** Retrieve parameter value as integer from request.
     * @param string $sName name of argument
     * @param string $iDef  optional default value
     * @return int parameter value
     */
    function getInt($sName, $iDef=0)
    {
        $iRes =  intVal($this->get($sName, $iDef));
        return $iRes ? $iRes : $iDef;
    }

    /** Retrieve parameter value as float from request.
     * @param string $sName name of argument
     * @param string $fDef  optional default value
     * @return float parameter value
     */
    function getFloat($sName, $fDef=0.0)
    {
        return floatVal($this->get($sName, $fDef));
    }

    /** Retrieve parameter value as array from request.
     * @param string $sName name of argument
     * @param string $fDef  optional default value
     * @return array parameter value
     */
    function getArray($sName, $mDef=array())
    {
        return (array)$this->get($sName, $mDef);
    }

    /**  Retrieve from request all arguments.
     * @return array
     */
    function &getAll($mDisallows = "")
    {
    	if ( $mDisallows )
    	{
    		$aArgs = $this->_aArgs;

    		$mDisallows[] = App::getParam('get_path_var');

    		foreach($mDisallows as $sDisAllow)
    		{
				if ( isset($aArgs[$sDisAllow]) )
				{
					unset($aArgs[$sDisAllow]);
				}
    		}

    		foreach($aArgs as $n => $v)
    		{
    			if ( is_numeric($n) )
    			{
    				unset($aArgs[$n]);
    			}
    		}

    		return $aArgs;
    	}

        return $this->_aArgs;
    }

    /** Retrieve parameter value as data (stored in string) from request.
     * @param string $sName name of argument
     * @param string $sDef  optional default value
     * @return string parameter value
     */
    function getStrDate($sName, $sDef='')
    {
        $sDate = $this->get($sName);
        $aMat=array();
        if (preg_match('/^([\d]{4})-([\d]{2})-([\d]{2})$/', $sDate, $aMat))
            $sDate = date('Y-m-d', mktime(0 ,0, 0, $aMat[2], $aMat[3], $aMat[1]));
        else
            $sDate = $sDef;

        return $sDate;
    }

    /** Retrieve parameter value as data (stored in 3 paramtere) from request.
     * @param string $sName prefix name of arguments
     * @param string $sDef  optional default value
     * @return string parameter value
     */
    function getArrDate($sName)
    {
        return sprintf('%04u-%02u-%02u', $this->get($sName.'Year'), $this->get($sName.'Month'), $this->get($sName.'Day'));
    }

    /** Returns remote ip address
     *  @return string ip address
     */
    function getIP()
    {
        $sAdr = $_SERVER["REMOTE_ADDR"];
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
            //$nPos = strpos($sAdr, ',');
            //$sAdr .= ' / ';
            //if ($nPos===false)
                $sAdr = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        return $sAdr;
    }

    /** Set parts for current area.
     * @param string $sArea  current area name
     * @param array  $aParts part configuration
     */
    function setAreaConfig($sArea, $aParts, $aMenu)
    {
        $this->_sArea  = $sArea;
        $this->_aParts = $aParts;
        $this->_aMenu  = $aMenu;

        $this->_sPart = $this->get('part');
        if (!$this->_sPart)
            $this->_sPart = key($this->_aParts);
        else //check part
            if (!isset($this->_aParts[$this->_sPart]))
                $this->gotoUrl($this->_sArea);

        //if matching menu -- mark as active (highlight menu)
        if (isset($this->_aMenu[$this->_sPart]))
            $this->_aMenu[$this->_sPart]['is_active'] = true;

        //get section
        $this->_sSect = $this->get('sect');
        if (!$this->_sSect)
            $this->_sSect = key($this->_aParts[$this->_sPart]['sections']);
        else //check section
            if (!isset($this->_aParts[$this->_sPart]['sections'][$this->_sSect]))
                $this->gotoUrl($this->_sArea);
    }

    function getSectionFile($sSuffix='php')
    {
        return $this->_sPart.'/'.$this->_sSect.'.'.$sSuffix;
    }

    function getSectionTitle()
    {
        return $this->_aParts[$this->_sPart]['title'];
    }

    function getMenu()
    {
        return $this->_aMenu;
    }

    /** Trims params and strip slashes if magic_quotes_gpc is set.
     * @param mixed $mParam request params
     * @return mixed trimmed params.
     */
    function _trim($mParam)
    {
    	if (is_array($mParam))
    		return array_map(array('Request', '_trim'), $mParam);

    	if (get_magic_quotes_gpc())
            $mParam = stripslashes($mParam);

    	return trim($mParam);
    }

}
?>